package de.dopichaj.labrador.search.prune.test;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import de.dopichaj.labrador.search.hit.Hit;
import de.dopichaj.labrador.search.hit.HitHierarchyTree;
import de.dopichaj.labrador.search.hit.MemoryHit;
import de.dopichaj.labrador.search.prune.ResultTreePruner;

import junit.framework.TestCase;

public abstract class PrunerTestCase extends TestCase {
    
    protected ResultTreePruner pruner;
    protected Collection<Hit> hitList;
    protected Set<Hit> expected;
    
    @Override
    public void setUp() {
        hitList = new ArrayList<Hit>();
        expected = new HashSet<Hit>();
    }
    
    /**
     * Prune the hits in {@link #hitList}.
     * @return
     *      the pruned results
     */
    protected Set<Hit> prune() {
        HitHierarchyTree tree = new HitHierarchyTree(hitList);
        pruner.prune(tree);
        return new HashSet<Hit>(tree.generateHitList());
    }
    
    private static final File PATH = new File("dummy");
    /**
     * Create a new hit with the given parameters and add it to hitList.
     */
    protected Hit makeHit(String path, float score, boolean isResult) {
        Hit hit = new MemoryHit(PATH, path);
        hit.setScore(score);
        hitList.add(hit);
        if (isResult) {
            expected.add(hit);
        }
        return hit;
    }
    
    protected Hit makeHit(String path, int length, int pos, float score, boolean isResult) {
        Hit hit = new MemoryHit(PATH, path, length, pos, score);
        hitList.add(hit);
        if (isResult) {
            expected.add(hit);
        }
        return hit;
    }
    
}
